package jp.dodododo.reloadable.servlet;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ServletWrapperContainer {
	private static final List<ServletWrapper> SERVLET_WRAPPER_LIST = new ArrayList<ServletWrapper>();

	public static void register(ServletWrapper servletWrapper) {
		SERVLET_WRAPPER_LIST.add(servletWrapper);
	}

	public static void remove(ServletWrapper servletWrapper) {
		SERVLET_WRAPPER_LIST.remove(servletWrapper);
	}

	public static void initAll() {
		for (Iterator<ServletWrapper> iter = SERVLET_WRAPPER_LIST.iterator(); iter.hasNext();) {
			ServletWrapper servletWrapper = iter.next();
			try {
				servletWrapper.initTarget();
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
	}

	public static void destroyAll() {
		for (Iterator<ServletWrapper> iter = SERVLET_WRAPPER_LIST.iterator(); iter.hasNext();) {
			ServletWrapper servletWrapper = iter.next();
			try {
				servletWrapper.destroyTarget();
			} catch (Throwable ignore) {
				ignore.printStackTrace();
			}
		}
	}

}
